<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Product controller
 *
 * @category   Mage
 * @package    Mage_Catalog
 */
require_once 'Mage/Catalog/controllers/ProductController.php';
class Verz_Home_Catalog_ProductController extends Mage_Catalog_ProductController
{
    /**
     * Product view action
     */
    public function mixAction()
    {
        $is_ajax = Mage::helper("home")->isAjax();
        
        //echo "gggg";
        
        if($is_ajax){
            
            $page = $this->getRequest()->getParam('page');
            $category_id = $this->getRequest()->getParam('category_id');
            if(!empty($category_id)){
                $cate_obj = Mage::getModel("home/catalog_category");
            
                $_cate = $cate_obj->load($category_id);
                $level = $_cate->getLevel();
                $result = array();
                $result ['error'] = 0;

                if($level==NUM_LEVEL_SHOW_PRODUCT){

                    $product_obj = Mage::getModel("home/catalog_product");

                    $product_list = $product_obj->getProductList($category_id, $page);
                    if(count($product_list) > 0){
                        $page_arr = $product_obj->getPagination($category_id,$page);

                        $cate_info = $this->getMixBreadcrumbs($category_id);


                        $this->loadLayout ( 'catalog_product_mix' );

                        $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'is_prod', 1 );
                        $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'page_arr', $page_arr );
                        $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'cate_info', $cate_info );     

                        $result ['result'] = $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'product_list', $product_list )->toHtml ();

                    }
                    else{
                        $result ['error'] = 1;
                        $result ['result'] = $this->__("This category doesn't contain data at a moment");
                    }
                }    
                else{

                    $this->loadLayout ( 'catalog_product_mix' );

                    $cate_list = $cate_obj->getChildrenCates($category_id,$page);

                    if(count($cate_list) > 0){

                        $page_arr = $cate_obj->getPagination($category_id,$page);

                        $cate_info = $this->getMixBreadcrumbs($category_id);

                        $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'page_arr', $page_arr );
                        $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'cate_info', $cate_info );     
                        $result ['result'] = $this->getLayout()->getBlock ( 'mix.list' )->setData ( 'cate_list', $cate_list )->toHtml ();

                    }
                    else{

                        $result ['error'] = 1;
                        $result ['result'] = $this->__("This category doesn't contain data at a moment");
                    }

                }

                $this->getResponse ()->setBody ( Mage::helper ( 'core' )->jsonEncode ( $result ) );
            }
            else{
                $searchstr = $this->getRequest()->getParam('searchstr');
                
                $product_obj = Mage::getModel("home/catalog_product");

                $product_list = $product_obj->getProductList(false, $page,$searchstr);
                if(count($product_list) > 0){

                    $page_arr = $product_obj->getPagination(false,$page,$searchstr);

                    //$cate_info = $this->getMixBreadcrumbs(fal);

                    $this->loadLayout ( 'catalog_product_mix' );

                    $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'is_prod', 1 );
                    $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'page_arr', $page_arr );
                    $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'cate_info', array() );     

                    $result ['result'] = $this->getLayout ()->getBlock ( 'mix.list' )->setData ( 'product_list', $product_list )->toHtml ();

                }
                else{
                    $result ['error'] = 1;
                    $result ['result'] = $this->__("There is no result for this key");
                }
                $this->getResponse ()->setBody ( Mage::helper ( 'core' )->jsonEncode ( $result ) );
            }
            
            
        }
        else{
            
            $this->loadLayout();
            $this->renderLayout();
            
            
        }
        
    }
    protected function getMixBreadcrumbs($categoryId){
        //createBlock($type, $name='', array $attributes = array())
        
        $cate_model = Mage::getModel("catalog/category");
        $root_category_id = Mage::app()->getStore()->getRootCategoryId();
        $cate_list = $cate_model->getChildrenCates($root_category_id);
        
        //$cate = $cate_model->load($root_category_id);
        $cate_ids  = array();
        if(count($cate_list) > 0){
            foreach($cate_list as $_cate){
                
                $cate_ids[] = $_cate->getId();
            }
        }
        
        $flag_category_id = $categoryId;
        
        $cate_info = array();
        $i = 0;
        
        $cate = $cate_model->load($flag_category_id);
        //$cate = $cate_model->load(22);
        
        $cate_info[$i]['id']   = $categoryId;
        $cate_info[$i]['name'] = $cate->getName();
        
        while(!in_array($flag_category_id, $cate_ids)){
            
            $parentId = $cate->getParentId();
            
            $flag_category_id  = $parentId;
            
            $i++;
            $cate_info[$i]['id'] = $flag_category_id;
            
            $cate_model->unsetData();
            
            $cate = $cate_model->load($flag_category_id);
            
            $cate_info[$i]['name'] = $cate->getName();        
            
        }
        
        return $cate_info;
    }
}
